package org.example.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.internal.org.objectweb.asm.TypeReference;
import lombok.SneakyThrows;
import org.example.entity.User;
import org.example.feign.ProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ProductClient productClient;

    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/test1")
    public String test1(Long id) {
        String info = productClient.getInfo(id);
        System.out.println(info);
        return "user ok! \n" + info;
    }

    @GetMapping("/test2")
    public String test2(String name) {
        String info = productClient.getName(name);
        System.out.println(info);
        return "user ok! \n" + info;
    }

    @GetMapping("/test3")
    public String test3(@RequestBody User user) {
        user.setUsername(user.getUsername());
        user.setPassword(user.getPassword());
        String userinfo = productClient.getUser(user);
        return "user ok! \n" + userinfo;
    }

    @GetMapping("/test4")
    public String test4(String[] arr) {
        String result = productClient.getArr(arr);
        return "user ok! \n" + result;
    }

    @GetMapping("/test5")
    public String test5() throws JsonProcessingException {
        User user = new User();
        user.setUsername("cyk");
        user.setPassword("1111");
        List<String> arrayList = new ArrayList<>();
        arrayList.add("aaa");
        arrayList.add("bbb");
        user.setArr(arrayList);
        String userList = productClient.getUserList(user);
        User user2 = objectMapper.readValue(userList, User.class);
        System.out.println(user2);
        return "user ok! \n" + userList;
    }

    @SneakyThrows
    @GetMapping("/test6")
    public String test6() {
        String data = productClient.getData();
        Long finalData = objectMapper.readValue(data, Long.class);
        return "user ok!" + finalData;
    }

}
